package com.spark.sniffer.tracing;

public class Span {
    public final static int DEFAULT_CODE = 200;
    public final static int ERR_CODE = 500;

    private Type type;
    private String name;
    private long beginTime = System.currentTimeMillis();
    private long endTime;
    private int code = DEFAULT_CODE;

    public Span() {}

    public Span(Type type, String name) {
        this.type = type;
        this.name = name;
    }

    public long getBeginTime() {
        return beginTime;
    }

    public Span setBeginTime(long beginTime) {
        this.beginTime = beginTime;
        return this;
    }

    public long getEndTime() {
        return endTime;
    }

    public Span setEndTime(long endTime) {
        this.endTime = endTime;
        return this;
    }

    public String getName() {
        return name;
    }

    public Span setName(String name) {
        this.name = name;
        return this;
    }

    public long getDuration() {
        return endTime - beginTime;
    }

    public int getCode() {
        return code;
    }

    public Span setCode(int code) {
        this.code = code;
        return this;
    }

    public Type getType() {
        return type;
    }

    public Span setType(Type type) {
        this.type = type;
        return this;
    }

    public boolean isSuccess() {
        return this.code == DEFAULT_CODE || this.code == 0;
    }

    public static enum Type {
        Request("uri"), OpenFeign("fein"), Redis("redis"), RestTemplate("rest"), MyBatis("db"), HttpClient("http"),
        EhCache("eh"), Scheduled("job"), MQConsumer("consumer"), MongoDB("mongo"),WS("ws"),Oss("oss");

        private String value;

        Type(String value) {
            this.value = value;
        }

        public String value() {
            return this.value;
        }
    }
}
